package furns.web;

import furns.entity.Member;
import furns.service.MemberService;
import furns.service.impl.MemberServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class RegisterServlet extends HttpServlet {
    private MemberService memberService = new MemberServiceImpl();
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取前端数据
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String email = req.getParameter("email");

        //判断是否存在用户名
        if (!memberService.isExistsUsername(username)){
            Member member = new Member(null, username, password, email,null);
            boolean register = memberService.registerMember(member);
            if (register){
                //注册成功，实现转发
                req.getRequestDispatcher("/views/member/register_ok.jsp").forward(req,resp);
            }else {
                //注册失败，实现转发
                req.getRequestDispatcher("/views/member/register_fail.jsp").forward(req,resp);
            }
        }else {
            //用户名存在，转发到登录界面
            req.getRequestDispatcher("/views/member/login.jsp").forward(req,resp);
        }
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }
}
